<!DOCTYPE html>
<!--
Copyright 2018 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/metrics/tabs_metric.html">
<link rel="import" href="/tracing/value/histogram_set.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  test('tabsMetric_tabSwitching', function() {
    const model = tr.c.TestUtils.newModel((model) => {
      const browserThread = setUpBrowserThread(model);
      addTabSwitchingData(browserThread);

      const rendererMainThread = setUpRendererMainThread(model);
      addTabSwitchingData(rendererMainThread);

      const group = browserThread.asyncSliceGroup;
      group.push(tr.c.TestUtils.newAsyncSliceEx({cat: 'latency',
        title: 'TabSwitchVisibilityRequest', start: 3, duration: 10}));
      group.push(tr.c.TestUtils.newAsyncSliceEx({cat: 'latency',
        title: 'TabSwitchVisibilityRequest', start: 15, duration: 6}));
    });
    const histograms = new tr.v.HistogramSet();
    tr.metrics.tabs.tabsMetric(histograms, model);
    const hist = histograms.getHistogramNamed('tab_switching_latency');
    assert.strictEqual(hist.max, 43.0);
    assert.strictEqual(hist.min, 24.0);
    assert.strictEqual(hist.average, 33.0);
    assert.strictEqual(hist.numValues, 6);
  });

  test('tabsMetric_tabSwitching_legacy', function() {
    const model = tr.c.TestUtils.newModel((model) => {
      const browserThread = setUpBrowserThread(model);
      addTabSwitchingData(browserThread, true);

      const group = browserThread.asyncSliceGroup;
      group.push(tr.c.TestUtils.newAsyncSliceEx({cat: 'latency',
        title: 'TabSwitchVisibilityRequest', start: 3, duration: 10}));
      group.push(tr.c.TestUtils.newAsyncSliceEx({cat: 'latency',
        title: 'TabSwitchVisibilityRequest', start: 15, duration: 6}));
    });
    const histograms = new tr.v.HistogramSet();
    tr.metrics.tabs.tabsMetric(histograms, model);
    const hist = histograms.getHistogramNamed('tab_switching_latency');
    assert.strictEqual(hist.max, 37.0);
    assert.strictEqual(hist.min, 14.0);
    assert.strictEqual(hist.average, 25.0);
    assert.strictEqual(hist.numValues, 3);
  });

  test('tabsMetric_tabSwitchRequestDelay', function() {
    const model = tr.c.TestUtils.newModel((model) => {
      const browserThread = setUpBrowserThread(model);
      const group = browserThread.asyncSliceGroup;
      group.push(tr.c.TestUtils.newAsyncSliceEx({cat: 'latency',
        title: 'TabSwitchVisibilityRequest', start: 0, duration: 10}));
      group.push(tr.c.TestUtils.newAsyncSliceEx({cat: 'latency',
        title: 'TabSwitchVisibilityRequest', start: 15, duration: 6}));
    });
    const histograms = new tr.v.HistogramSet();
    tr.metrics.tabs.tabsMetric(histograms, model);
    const hist = histograms.getHistogramNamed('tab_switching_request_delay');
    assert.strictEqual(hist.average, 8);
  });

  function setUpBrowserThread(model) {
    const BROWSER_PROCESS_ID = 1234;
    const browserProcess = model.getOrCreateProcess(BROWSER_PROCESS_ID);
    const browserThread = browserProcess.getOrCreateThread(2);
    browserThread.name = 'CrBrowserMain';
    return browserThread;
  }

  function setUpRendererMainThread(model) {
    const RENDERER_PROCESS_ID = 2345;
    const rendererProcess = model.getOrCreateProcess(RENDERER_PROCESS_ID);
    const mainThread = rendererProcess.getOrCreateThread(23);
    mainThread.name = 'CrRendererMain';
    return mainThread;
  }

  function addTabSwitchingData(thread, legacy) {
    const group = thread.asyncSliceGroup;
    group.push(tr.c.TestUtils.newAsyncSliceEx({
      cat: 'latency',
      title: 'TabSwitching::Latency',
      start: 1,
      duration: 24,
      args: legacy ? {} : {latency: 24 },
    }));
    group.push(tr.c.TestUtils.newAsyncSliceEx({
      cat: 'latency',
      title: 'TabSwitching::Latency',
      start: 2.5,
      duration: 24,
      args: legacy ? {} : {latency: 24 },
    }));
    group.push(tr.c.TestUtils.newAsyncSliceEx({
      cat: 'latency',
      title: 'TabSwitching::Latency',
      start: 5,
      duration: 14,
      args: legacy ? {} : {latency: 32 },
    }));
    group.push(tr.c.TestUtils.newAsyncSliceEx({
      cat: 'latency',
      title: 'TabSwitching::Latency',
      start: 6,
      duration: 37,
      args: legacy ? {} : {latency: 43 },
    }));
  }
});
</script>
